package webapi

import (
	"ims/app/dao"
	"ims/app/ws"
	"ims/tools/response"

	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var Service = serviceApi{}

type serviceApi struct{}

// /api/kefu/service/info 接口返回当前登录的客服信息
func (*serviceApi) GetServiceInfo(r *ghttp.Request) {
	// kefuId 这个值，从中间件mideware 里面解析 请求头header 里面的bear 得到
	kefuId := r.GetString("kefuId")
	info := dao.GetChatServiceByIdOrUserId(kefuId, kefuId)
	//转换为json对象进行增加 键值操作
	kefuInfo := gjson.New(info)
	//append 作为数组添加
	kefuInfo.Append("user_ids", info.UserId)
	kefuInfo.Set("site_name", "多米客服")

	response.JsonResponse(r, g.Map{
		"status": 200,
		"msg":    "ok",
		"data":   kefuInfo,
	})
}

// /api/kefu/service/list
// 返回当前客服和某个访客的对话记录
func (*serviceApi) GetChatList(r *ghttp.Request) {
	userId := r.GetInt("user_id", 0)
	upperId := r.GetInt("upperId", 0)
	// is_tourist := r.GetInt("is_tourist", 0)
	// page := r.GetInt("page", 1)
	limit := r.GetInt("limit", 15)

	if userId == 0 {
		response.JsonResponse(r, g.Map{
			"status": 404,
			"msg":    "缺少参数",
			"data":   userId,
		})
	}

	// g.Log().Debug(upperId, is_tourist, page, limit)
	record := dao.GetChatDialogueRecordList(ws.APPID, userId, limit, upperId)

	response.JsonResponse(r, g.Map{
		"status": 200,
		"msg":    "ok",
		"data":   record,
	})
}

// api/mobile/service/adv
//获取客服页面的广告信息
func (*serviceApi) GetKfAdv(r *ghttp.Request) {
	response.JsonResponse(r, g.Map{
		"status": 200,
		"msg":    "ok",
		"data":   dao.ChatCache.GetKfAdv(),
	})
}
